﻿using System;
using SysHook.Cilpboard;
using System.Windows.Forms;
using SysHook;

namespace ClipboardSecure
{
    internal class ClipCenter : IDisposable
    {
        internal string last_clipboard { get; set; }
        internal long clipboard_times { get; set; }
        internal RecordCenter record { get; set; }
        internal ClipCenter() 
        {
            record = new RecordCenter();
        }
        internal void Start()
        {
            ClipboardMonitor.Start();
            ClipboardMonitor.OnClipboardChange += ClipboardMonitor_OnClipboardChange; ;
        }

        private void ClipboardMonitor_OnClipboardChange(object sender)
        {
            var text = "";
            var a = Clipboard.GetDataObject();
            if (a.GetDataPresent(DataFormats.UnicodeText))
            {
                text = a.GetData(DataFormats.UnicodeText) as string;
            }
            ClearClipboard(text);
        }

        private void ClearClipboard(string text)
        {
            Clipboard.Clear();
            if (text.Trim() == "" || text == last_clipboard) return;
            last_clipboard = text;
            clipboard_times++;
            record.Record(text);
            if (GlobalSettings.beep_sound) SoundHook.PlayBeep();
        }

        public void Dispose()
        {
            ClipboardMonitor.Stop();
            record.Dispose();
        }
    }
}
